package com.lxm.enigma.jsonresult;

import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@Data
public class JsonResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;


    private int code = 0;

    private String message = "";

    private T data;

    private String sign;

    private JsonResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static JsonResult json() {
        return json(0, "ok");
    }

    public static JsonResult json(int code, String message) {
        return json(code, message, null);
    }

    public static JsonResult json(int code, String message, Object data) {
        return new JsonResult(code, message, data);
    }

    public static JsonResult json(Map<String, Object> map) {
        int code = 0;
        if (map.containsKey("code")) {
            code = (int) map.get("code");
            map.remove("code");
        }
        String message = "";
        if (map.containsKey("message")) {
            message = (String) map.get("message");
            map.remove("message");
        }
        return json(code, message, map);
    }

    public static JsonResult select2(int code, List<?> list) {
        return json(code, "", list);

    }

    public static JsonResult select2(List<?> list) {
        return select2(0, list);
    }



}
